package org.abratuhi.kvngroupware.dalgen;

import java.util.List;

import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageProceeder;
import org.abratuhi.kvngroupware.widget.WidgetManager;
import org.jdom.Element;

public class DalgenResponseMessageProceeder implements MessageProceeder{
	DalgenManager dalgenmanager;
	WidgetManager widgetmanager;
	
	public DalgenResponseMessageProceeder(DalgenManager dalgenmanager, WidgetManager widgetmanager){
		this.dalgenmanager = dalgenmanager;
		this.widgetmanager = widgetmanager;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void proceedMessage(Client client, Message message) {
		Element response = ((Element) message.getRoot().getChildren("response").get(0));
		String type = response.getAttributeValue("type");
		String action = response.getAttributeValue("action");
		String status = response.getAttributeValue("status");
		
		if(type.equals("dalgen") &&
				action.equals("get") &&
				status.equals("ok")){
			dalgenmanager.clear();
			List<Element> words = response.getChildren("word");
			for(int i=0; i<words.size(); i++){
				String word = words.get(i).getText();
				dalgenmanager.put(word, word);
			}
			widgetmanager.get("dalgen").getWindow().repaint();
		}
		
	}

}
